package com.macro.mall.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.mapper.UmsMemberReceiveAddressMapper;
import com.macro.mall.mapper.UmsMemberReceiveAddressOtherMapper;
import com.macro.mall.model.UmsMember;
import com.macro.mall.model.UmsMemberReceiveAddress;
import com.macro.mall.model.UmsMemberReceiveAddressExample;
import com.macro.mall.portal.service.UmsMemberReceiveAddressService;
import com.macro.mall.portal.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 用户地址管理Service实现类
 */
@Service
public class UmsMemberReceiveAddressServiceImpl implements UmsMemberReceiveAddressService {
    @Autowired
    private UmsMemberService memberService;
    @Autowired
    private UmsMemberReceiveAddressMapper addressMapper;
    @Autowired
    private UmsMemberReceiveAddressOtherMapper addressOtherMapper;

    @Override
    public int add(UmsMemberReceiveAddress address) {
        UmsMember currentMember = memberService.getCurrentMember();
        address.setMemberId(currentMember.getId());
        updateDefault(address.getDefaultStatus(), currentMember);
        return addressMapper.insert(address);
    }

    @Override
    public int delete(Long id) {
        UmsMember currentMember = memberService.getCurrentMember();
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        example.createCriteria().andMemberIdEqualTo(currentMember.getId()).andIdEqualTo(id);
        return addressMapper.deleteByExample(example);
    }

    @Override
    public int update(UmsMemberReceiveAddress address) {
        long id = address.getId();
        address.setId(null);
        UmsMember currentMember = memberService.getCurrentMember();
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        example.createCriteria().andMemberIdEqualTo(currentMember.getId()).andIdEqualTo(id);
        updateDefault(address.getDefaultStatus(), currentMember);
        return addressMapper.updateByExampleSelective(address, example);
//        address.setId(id);
//        return addressMapper.updateByPrimaryKeySelective(address);
    }

    @Override
    public List<UmsMemberReceiveAddress> list(Long memberId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
//        UmsMember currentMember = memberService.getCurrentMember();
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        example.createCriteria().andMemberIdEqualTo(memberId);
        example.setOrderByClause("default_status desc");
        return addressMapper.selectByExample(example);
    }

    @Override
    public UmsMemberReceiveAddress getDefault() {
        UmsMember currentMember = memberService.getCurrentMember();
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        example.createCriteria().andMemberIdEqualTo(currentMember.getId()).andDefaultStatusEqualTo(1);
        List<UmsMemberReceiveAddress> umsMemberReceiveAddresses = addressMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(umsMemberReceiveAddresses)) {
            return umsMemberReceiveAddresses.get(0);
        } else {
            return new UmsMemberReceiveAddress();
        }
    }

    @Override
    public UmsMemberReceiveAddress getItem(Long id) {
        UmsMember currentMember = memberService.getCurrentMember();
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        example.createCriteria().andMemberIdEqualTo(currentMember.getId()).andIdEqualTo(id);
        List<UmsMemberReceiveAddress> addressList = addressMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(addressList)) {
            return addressList.get(0);
        }
        return null;
    }

    @Override
    public CommonResult updateDefaultAddress(long id) {
        UmsMemberReceiveAddress currentAddress = addressMapper.selectByPrimaryKey(id);
        currentAddress.setDefaultStatus(1);//修改当前地址为默认地址：状态置为1
        int count = addressMapper.updateByPrimaryKey(currentAddress);
        Long memberId = currentAddress.getMemberId();
        addressOtherMapper.updateDefault(memberId, 1, 0, id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 把之前的默认地址修改
     * @param defaultStatus
     * @param currentMember
     */
    private void updateDefault(Integer defaultStatus, UmsMember currentMember){
        if (defaultStatus != null && defaultStatus == 1) {
            //先将原来的默认地址去除
            UmsMemberReceiveAddress record = new UmsMemberReceiveAddress();
            record.setDefaultStatus(0);
            UmsMemberReceiveAddressExample updateExample = new UmsMemberReceiveAddressExample();
            updateExample.createCriteria()
                    .andMemberIdEqualTo(currentMember.getId())
                    .andDefaultStatusEqualTo(1);
            addressMapper.updateByExampleSelective(record, updateExample);
        }
    }
}
